package es.tid.www.wsdl.parlayx.third_party_call.v1_1.consumer;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class ThirdPartyCallConfigInfo {
	
	/**
	 * path for config properties file for this application
	 */
	private static final String CONFIG_FILE_PATH = "/conf/config.properties";
	/**
	 * object with all config parameters readed from file
	 */
	private Properties properties;
	/**
	 * unique instance with all config parameters loaded in memory
	 */
	private static ThirdPartyCallConfigInfo instance;
	
	/**
	 * Creates a new instance for config object. This object remains in memory until application server is shutdown.
	 * Loads all configuration from config.properties file and stores it in a Properties object
	 */
	private ThirdPartyCallConfigInfo(){
		properties=new Properties();
	}
	
	/**
	 * returns the unique instance for configuration properties. If the instance is not created yet, 
	 * its created and read all config.properties configuration file and loaded in memory
	 * @return The unique instance with all application configuration properties
	 */
	public static ThirdPartyCallConfigInfo getInstance() throws Exception{
		if (instance==null){
			instance=new ThirdPartyCallConfigInfo();
			instance.loadConfigPropertiesFile(CONFIG_FILE_PATH);
		}
		return instance;
	}
	
	/**
	 * loads a properties file and loads it in memory to be used into the application logic
	 * @param filePath path where find the properties file to read
	 */
	private void loadConfigPropertiesFile(String filePath) throws IOException{
		FileInputStream fileInputStream=null;
		try
		{
			//first, tries to load the configuration properties from a config file in this class same context
			//(if this class is into a .jar library, tries to search config.properties into the jar file)
			properties.load(this.getClass().getClassLoader().getResourceAsStream(filePath));
		}
		catch (Exception exc)
		{
			try{
				//if first load try doesn't work. It tries to load the config parameters 
				//from application general context 
				properties.load(this.getClass().getResourceAsStream(filePath));
			}
			catch (Exception e){
				//if not possible to get resource as stream, try to get it directly from the path
				//this class was loaded in local file system
				try{
					fileInputStream=new FileInputStream(new File("."+filePath));
					properties.load(fileInputStream);
				}
				catch (Exception ex){
					throw new IOException("Exception reading config properties file: "+ex.getMessage());
				}
				finally{
					//if properties were loaded successfully, close the open streams and files
					if (fileInputStream!=null){
						try{
							fileInputStream.close();
						}catch (Exception ex){
							throw new IOException("Exception closing stream for reading config properties file: "+ex.getMessage());
						}
					}
				}
			}
		}
	}
	
	/**
	 * Gets a configuration value. Returns the value related to the key received as parameter
	 * @param key key name of the property whose value is going to be returned
	 * @return the value related to the key received as parameter
	 */
	public String getConfigProperty(String key){
		return properties.getProperty(key);
	}

}
